package com.zheng.controller;
import com.alibaba.fastjson.JSON;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.zheng.mapper.UserMapper;
import com.zheng.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * RequestBody:用于后端接收前端的json字符串，主要适用于post请求
 * RequestParam：
 * swagger2:默认
 */
@Api("LoginController用户登录控制类")
@ApiOperation("用户登录")
@RestController
public class LoginController {
    /*验证码*/
    private String capText;
    @Autowired
    public UserMapper userMapper;
    @Autowired
    private DefaultKaptcha defaultKaptcha;

    /*解决图片验证码问题*/
    @ApiOperation("图形验证码")
    @GetMapping(value = "/captcha",produces = "image/jpeg")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        request.getSession().setAttribute("i",123);
        //获取验证码文本内容
        capText = defaultKaptcha.createText();
        System.out.println("验证码内容" + capText);
        request.getSession().setAttribute("text",capText);
        //根据文本验证码创建图形验证码
        BufferedImage image = defaultKaptcha.createImage(capText);
        ServletOutputStream outputStream = response.getOutputStream();
        ImageIO.write(image,"jpg",outputStream);
        outputStream.close();
    }
    /*解决跨域问题*/
    @ApiOperation("登录")
    @CrossOrigin
    @PostMapping("/toLogin")
    public String toLogin(@RequestBody User user){
        System.out.println(user);
        System.out.println(user.getCode());
        System.out.println(capText);
        System.out.println("用户名为:" + user.getUsername() + "密码为:" + user.getPassword() );
        //对数据库进行查询
        User user1 = userMapper.getUser(user.getUsername(), user.getPassword());
        Map<String,Object> res = new HashMap<>();
        String flag = "error";
        /*如果用户输入的用户名在数据库中存在*/
        if(user1 != null ){
            /*获取验证码信息*/
            /*如果输入的验证码和图片验证码一致，则返回ok，否则返回exception*/
            if(user.getCode().equals(capText)){
                flag = "ok";
            }
            else{
                flag = "exception";
            }
        }
        /*用户输入的用户名在数据库中不存在,或者密码输入错误*/
        else{
            flag = "notExit";
        }
        res.put("flag",flag);
        res.put("user",user1);
        //将一个对象封装成一个json字符串
        String res_json = JSON.toJSONString(res);
        System.out.println(res_json);
        //将该字段返回前端
        return res_json;
    }

}
